#!/bin/sh

# run from directory where this script is
cd `dirname $0`
EXAMPLE_DIR=`pwd`

# check whether echo has the -e option
if test "`echo -e`" = "-e" ; then ECHO=echo ; else ECHO="echo -e" ; fi

$ECHO
$ECHO "$EXAMPLE_DIR : starting"
$ECHO
$ECHO "This example shows how to use pw.x with self-interaction correction for" 
$ECHO "localizing the hole polaron in MgO and calculating its energetics. "

# set the needed environment variables
. ../../../environment_variables

# required executables and pseudopotentials
BIN_LIST="pw.x"
PSEUDO_LIST="Mg.pbe-mt_fhi.UPF O.pbe-mt_fhi.UPF"

$ECHO
$ECHO "  executables directory: $BIN_DIR"
$ECHO "  pseudo directory:      $PSEUDO_DIR"
$ECHO "  temporary directory:   $TMP_DIR"
$ECHO "  checking that needed directories and files exist...\c"

# check for directories
for DIR in "$BIN_DIR" "$PSEUDO_DIR" ; do
    if test ! -d $DIR ; then
        $ECHO
        $ECHO "ERROR: $DIR not existent or not a directory"
        $ECHO "Aborting"
        exit 1
    fi
done
for DIR in "$TMP_DIR" "$EXAMPLE_DIR/reference" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done
cd $EXAMPLE_DIR/reference

# check for executables
for FILE in $BIN_LIST ; do
    if test ! -x $BIN_DIR/$FILE ; then
        $ECHO
        $ECHO "ERROR: $BIN_DIR/$FILE not existent or not executable"
        $ECHO "Aborting"
        exit 1
    fi
done

# check for pseudopotentials
for FILE in $PSEUDO_LIST ; do
    if test ! -r $PSEUDO_DIR/$FILE ; then
       $ECHO
       $ECHO "Downloading $FILE to $PSEUDO_DIR...\c"
            $WGET $PSEUDO_DIR/$FILE $NETWORK_PSEUDO/$FILE 2> /dev/null
    fi
    if test $? != 0; then
        $ECHO
        $ECHO "ERROR: $PSEUDO_DIR/$FILE not existent or not readable"
        $ECHO "Aborting"
        exit 1
    fi
done
$ECHO " done"


# how to run executables
PW_COMMAND="$PARA_PREFIX $BIN_DIR/pw.x $PARA_POSTFIX"

# Print how we run executables
$ECHO
$ECHO "  running pw.x as: $PW_COMMAND"
$ECHO

#
# Example 1: gammaDFT calculation with energy evaluation
#
cat > MgO_1.in << EOF
&CONTROL           
    calculation       =  'scf'
    restart_mode      =  'from_scratch'
    prefix            =  'MgO'
    pseudo_dir        =  '$PSEUDO_DIR'
    outdir            =  '$TMP_DIR'
    etot_conv_thr     =  1.0d-7
    forc_conv_thr     =  1.0d-5
    wf_collect        =  .false.
    max_seconds       =  86000
/
&SYSTEM            
    input_dft         =  'PBE'
    ecutwfc           =  100
    ecutrho           =  800
    tot_charge        =  +1
    ibrav             =  1
    A                 =  8.449313
    B                 =  8.449313
    C                 =  8.449313
    cosBC             =  0.00
    cosAC             =  0.00
    cosAB             =  0.00 
    nat               =  64
    ntyp              =  2
    occupations       =  'fixed'
    nbnd              =  150
    nspin             =  2
    tot_magnetization =  1
    sic_gamma         =  1.96
    sic_energy        =  .true.
    pol_type          =  'h'
/
&ELECTRONS         
    conv_thr          =  1.0d-7
    electron_maxstep  =  500
    mixing_beta       =  0.7
    diagonalization   =  'david'
/
ATOMIC_SPECIES     
    Mg  24.305   Mg.pbe-mt_fhi.UPF
    O   15.999   O.pbe-mt_fhi.UPF
K_POINTS           automatic
    1 1 1 0 0 0
ATOMIC_POSITIONS   angstrom 
Mg       0.004457614   0.004483216   0.004465068
Mg      -0.122100120   2.137583755   2.137601424
Mg       2.137594723  -0.122089896   2.137575082
Mg       2.137610497   2.137571073  -0.122092779
O        0.023122051   0.023117448   2.118442751
O        2.118445477   0.023119259   0.023120991
O        0.023113306   2.118442603   0.023109125
O        2.111518587   2.111535935   2.111526517
Mg      -0.006048852  -0.006046391   4.230895732
Mg      -0.020560163   2.112928690   6.339544083
Mg       2.112935562  -0.020548789   6.339518823
Mg       2.085468710   2.085453303   4.345145368
O        0.005810337   0.005799469   6.341255197
O        2.111528339   0.002185271   4.220859826
O        0.002189740   2.111531305   4.220871670
O        2.111522505   2.111531196   6.336180239
Mg      -0.006056267   4.230912294  -0.006050562
Mg      -0.020559679   6.339535524   2.112930321
Mg       2.085476916   4.345150163   2.085451919
Mg       2.112937584   6.339523919  -0.020553331
O        0.002186527   4.220859261   2.111520203
O        2.111521024   4.220859023   0.002185272
O        0.005811836   6.341271590   0.005806457
O        2.111524788   6.336186569   2.111530638
Mg      -0.007854775   4.229103495   4.229100106
Mg      -0.008262893   6.338608707   6.338603404
Mg       2.110123147   4.243616682   6.332816133
Mg       2.110125076   6.332824982   4.243609372
O        0.002400303   4.220655801   6.336178771
O        2.104613189   4.199941764   4.199936179
O        0.002399986   6.336184768   4.220650077
O        2.111527118   6.336192960   6.336185892
Mg       4.230893995  -0.006045964  -0.006044984
Mg       4.345144501   2.085466142   2.085460497
Mg       6.339538226  -0.020557823   2.112930437
Mg       6.339553629   2.112930593  -0.020562945
O        4.220870359   0.002183076   2.111526766
O        6.341255217   0.005801500   0.005807101
O        4.220880556   2.111530322   0.002183199
O        6.336180947   2.111526253   2.111523346
Mg       4.229101764  -0.007845214   4.229099917
Mg       4.243611032   2.110119299   6.332825776
Mg       6.338613485  -0.008261110   6.338605647
Mg       6.332834796   2.110120806   4.243603162
O        4.220658627   0.002395542   6.336180843
O        6.336185633   0.002397156   4.220652108
O        4.199935376   2.104608260   4.199927545
O        6.336179729   2.111527903   6.336169873
Mg       4.229099841   4.229102926  -0.007844686
Mg       4.243608663   6.332836189   2.110117107
Mg       6.332839896   4.243608359   2.110117005
Mg       6.338612826   6.338609852  -0.008262843
O        4.199939409   4.199934437   2.104605685
O        6.336180798   4.220656734   0.002395740
O        4.220657999   6.336184883   0.002394005
O        6.336184285   6.336182482   2.111524744
Mg       4.218579663   4.218588312   4.218583794
Mg       4.231317426   6.333757235   6.333750568
Mg       6.333760097   4.231315279   6.333751770
Mg       6.333760616   6.333758828   4.231311791
O        4.217249406   4.217245939   6.331103566
O        6.331101833   4.217247560   4.217242123
O        4.217251703   6.331108201   4.217242229
O        6.336185706   6.336183862   6.336177923
EOF

$ECHO "  running the gammaDFT calculation with energy evaluation ...\c"
$PW_COMMAND < MgO_1.in > MgO_1.out
check_failure $?
$ECHO " done"

# clean TMP_DIR
$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/MgO*
$ECHO " done"

#
# Example 2: gammaDFT atomic relaxation without energy evaluation
#
cat > MgO_2.in << EOF
&CONTROL           
    calculation       =  'relax'
    restart_mode      =  'from_scratch'
    prefix            =  'MgO'
    pseudo_dir        =  '$PSEUDO_DIR'
    outdir            =  '$TMP_DIR'
    etot_conv_thr     =  1.0d-7
    forc_conv_thr     =  1.0d-5
    wf_collect        =  .false.
    !max_seconds      =  86000
/
&SYSTEM            
    input_dft         =  'PBE'
    ecutwfc           =  100
    ecutrho           =  800
    tot_charge        =  +1
    ibrav             =  1
    A                 =  8.449313
    B                 =  8.449313
    C                 =  8.449313
    cosBC             =  0.00
    cosAC             =  0.00
    cosAB             =  0.00 
    nat               =  64
    ntyp              =  2
    occupations       =  'fixed'
    nbnd              =  150
    nspin             =  2
    tot_magnetization =  1
    sic_gamma         =  1.96
    sic_energy        =  .false.
    pol_type          =  'h'
/
&ELECTRONS         
    conv_thr          =  1.0d-7
    electron_maxstep  =  500
    mixing_beta       =  0.7
    diagonalization   =  'david'
/
&IONS              
    ion_dynamics      =  'damp'
/
ATOMIC_SPECIES     
    Mg  24.305   Mg.pbe-mt_fhi.UPF
    O   15.999   O.pbe-mt_fhi.UPF
K_POINTS           automatic
    1 1 1 0 0 0
ATOMIC_POSITIONS   angstrom 
Mg       0.004457614   0.004483216   0.004465068
Mg      -0.122100120   2.137583755   2.137601424
Mg       2.137594723  -0.122089896   2.137575082
Mg       2.137610497   2.137571073  -0.122092779
O        0.023122051   0.023117448   2.118442751
O        2.118445477   0.023119259   0.023120991
O        0.023113306   2.118442603   0.023109125
O        2.111518587   2.111535935   2.111526517
Mg      -0.006048852  -0.006046391   4.230895732
Mg      -0.020560163   2.112928690   6.339544083
Mg       2.112935562  -0.020548789   6.339518823
Mg       2.085468710   2.085453303   4.345145368
O        0.005810337   0.005799469   6.341255197
O        2.111528339   0.002185271   4.220859826
O        0.002189740   2.111531305   4.220871670
O        2.111522505   2.111531196   6.336180239
Mg      -0.006056267   4.230912294  -0.006050562
Mg      -0.020559679   6.339535524   2.112930321
Mg       2.085476916   4.345150163   2.085451919
Mg       2.112937584   6.339523919  -0.020553331
O        0.002186527   4.220859261   2.111520203
O        2.111521024   4.220859023   0.002185272
O        0.005811836   6.341271590   0.005806457
O        2.111524788   6.336186569   2.111530638
Mg      -0.007854775   4.229103495   4.229100106
Mg      -0.008262893   6.338608707   6.338603404
Mg       2.110123147   4.243616682   6.332816133
Mg       2.110125076   6.332824982   4.243609372
O        0.002400303   4.220655801   6.336178771
O        2.104613189   4.199941764   4.199936179
O        0.002399986   6.336184768   4.220650077
O        2.111527118   6.336192960   6.336185892
Mg       4.230893995  -0.006045964  -0.006044984
Mg       4.345144501   2.085466142   2.085460497
Mg       6.339538226  -0.020557823   2.112930437
Mg       6.339553629   2.112930593  -0.020562945
O        4.220870359   0.002183076   2.111526766
O        6.341255217   0.005801500   0.005807101
O        4.220880556   2.111530322   0.002183199
O        6.336180947   2.111526253   2.111523346
Mg       4.229101764  -0.007845214   4.229099917
Mg       4.243611032   2.110119299   6.332825776
Mg       6.338613485  -0.008261110   6.338605647
Mg       6.332834796   2.110120806   4.243603162
O        4.220658627   0.002395542   6.336180843
O        6.336185633   0.002397156   4.220652108
O        4.199935376   2.104608260   4.199927545
O        6.336179729   2.111527903   6.336169873
Mg       4.229099841   4.229102926  -0.007844686
Mg       4.243608663   6.332836189   2.110117107
Mg       6.332839896   4.243608359   2.110117005
Mg       6.338612826   6.338609852  -0.008262843
O        4.199939409   4.199934437   2.104605685
O        6.336180798   4.220656734   0.002395740
O        4.220657999   6.336184883   0.002394005
O        6.336184285   6.336182482   2.111524744
Mg       4.218579663   4.218588312   4.218583794
Mg       4.231317426   6.333757235   6.333750568
Mg       6.333760097   4.231315279   6.333751770
Mg       6.333760616   6.333758828   4.231311791
O        4.217249406   4.217245939   6.331103566
O        6.331101833   4.217247560   4.217242123
O        4.217251703   6.331108201   4.217242229
O        6.336185706   6.336183862   6.336177923

EOF

$ECHO "  gammaDFT atomic relaxation without energy evaluation ...\c"
$PW_COMMAND < MgO_2.in > MgO_2.out
check_failure $?
$ECHO " done"

# clean TMP_DIR
$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/MgO*
$ECHO " done"

#
# Example 3: gammaDFT calculation + scissor operator for bands
#
cat > MgO_3.in << EOF
&CONTROL           
    calculation       =  'scf'
    restart_mode      =  'from_scratch'
    prefix            =  'MgO'
    pseudo_dir        =  '$PSEUDO_DIR'
    outdir            =  '$TMP_DIR'
    etot_conv_thr     =  1.0d-7
    forc_conv_thr     =  1.0d-5
    wf_collect        =  .false.
    max_seconds       =  86000
/
&SYSTEM            
    input_dft         =  'PBE'
    ecutwfc           =  100
    ecutrho           =  800
    tot_charge        =  +1
    ibrav             =  1
    A                 =  8.449313
    B                 =  8.449313
    C                 =  8.449313
    cosBC             =  0.00
    cosAC             =  0.00
    cosAB             =  0.00 
    nat               =  64
    ntyp              =  2
    occupations       =  'fixed'
    nbnd              =  150
    nspin             =  2
    tot_magnetization =  1
    sic_gamma         =  1.96
    sic_energy        =  .false.
    pol_type          =  'h'
    sci_vb            =  0
    sci_cb            =  +1.0
/
&ELECTRONS         
    conv_thr          =  1.0d-7
    electron_maxstep  =  500
    mixing_beta       =  0.7
    diagonalization   =  'david'
/
ATOMIC_SPECIES     
    Mg  24.305   Mg.pbe-mt_fhi.UPF
    O   15.999   O.pbe-mt_fhi.UPF
K_POINTS           automatic
    1 1 1 0 0 0
ATOMIC_POSITIONS   angstrom 
Mg       0.004457614   0.004483216   0.004465068
Mg      -0.122100120   2.137583755   2.137601424
Mg       2.137594723  -0.122089896   2.137575082
Mg       2.137610497   2.137571073  -0.122092779
O        0.023122051   0.023117448   2.118442751
O        2.118445477   0.023119259   0.023120991
O        0.023113306   2.118442603   0.023109125
O        2.111518587   2.111535935   2.111526517
Mg      -0.006048852  -0.006046391   4.230895732
Mg      -0.020560163   2.112928690   6.339544083
Mg       2.112935562  -0.020548789   6.339518823
Mg       2.085468710   2.085453303   4.345145368
O        0.005810337   0.005799469   6.341255197
O        2.111528339   0.002185271   4.220859826
O        0.002189740   2.111531305   4.220871670
O        2.111522505   2.111531196   6.336180239
Mg      -0.006056267   4.230912294  -0.006050562
Mg      -0.020559679   6.339535524   2.112930321
Mg       2.085476916   4.345150163   2.085451919
Mg       2.112937584   6.339523919  -0.020553331
O        0.002186527   4.220859261   2.111520203
O        2.111521024   4.220859023   0.002185272
O        0.005811836   6.341271590   0.005806457
O        2.111524788   6.336186569   2.111530638
Mg      -0.007854775   4.229103495   4.229100106
Mg      -0.008262893   6.338608707   6.338603404
Mg       2.110123147   4.243616682   6.332816133
Mg       2.110125076   6.332824982   4.243609372
O        0.002400303   4.220655801   6.336178771
O        2.104613189   4.199941764   4.199936179
O        0.002399986   6.336184768   4.220650077
O        2.111527118   6.336192960   6.336185892
Mg       4.230893995  -0.006045964  -0.006044984
Mg       4.345144501   2.085466142   2.085460497
Mg       6.339538226  -0.020557823   2.112930437
Mg       6.339553629   2.112930593  -0.020562945
O        4.220870359   0.002183076   2.111526766
O        6.341255217   0.005801500   0.005807101
O        4.220880556   2.111530322   0.002183199
O        6.336180947   2.111526253   2.111523346
Mg       4.229101764  -0.007845214   4.229099917
Mg       4.243611032   2.110119299   6.332825776
Mg       6.338613485  -0.008261110   6.338605647
Mg       6.332834796   2.110120806   4.243603162
O        4.220658627   0.002395542   6.336180843
O        6.336185633   0.002397156   4.220652108
O        4.199935376   2.104608260   4.199927545
O        6.336179729   2.111527903   6.336169873
Mg       4.229099841   4.229102926  -0.007844686
Mg       4.243608663   6.332836189   2.110117107
Mg       6.332839896   4.243608359   2.110117005
Mg       6.338612826   6.338609852  -0.008262843
O        4.199939409   4.199934437   2.104605685
O        6.336180798   4.220656734   0.002395740
O        4.220657999   6.336184883   0.002394005
O        6.336184285   6.336182482   2.111524744
Mg       4.218579663   4.218588312   4.218583794
Mg       4.231317426   6.333757235   6.333750568
Mg       6.333760097   4.231315279   6.333751770
Mg       6.333760616   6.333758828   4.231311791
O        4.217249406   4.217245939   6.331103566
O        6.331101833   4.217247560   4.217242123
O        4.217251703   6.331108201   4.217242229
O        6.336185706   6.336183862   6.336177923
EOF

$ECHO "  gammaDFT calculation + scissor operator for bands ...\c"
$PW_COMMAND < MgO_3.in > MgO_3.out
check_failure $?
$ECHO " done"

# clean TMP_DIR
$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/MgO*
$ECHO " done"

$ECHO
$ECHO "$EXAMPLE_DIR : done"
